<template>
    <view class="headView">
           
        <view class="headImageView">
                 
            <image class="headImage" src="/static/wxz_wzbagent/resource/images/logo1.jpg" mode="scaleToFill"></image>
               
        </view>
           
        <view class="titleText">申请获取以下权限</view>
           
        <view class="contentText">获得你的公开信息(昵称,头像,地区,性别等)</view>
           
        <button class="authBtn" type="primary" @tap="bindGetUserInfo">授权登录</button>
    </view>
</template>

<script>
// wxz_wzbagent/pages/login/login.js
var app = getApp();
export default {
    data() {
        return {
            canIUse: uni.canIUse('button.open-type.getUserInfo'),
            callback_url: '',
            token: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        var that = this;
        var callback_url = options.callback_url;
        var rid = options.rid;
        var live_id = options.live_id;
        var token = options.token;
        that.setData({
            callback_url: callback_url,
            token: token
        });

        //     //查看是否授权
        //     wx.getSetting({
        //       success: function(res) {
        //         if (res.authSetting['scope.userInfo']) {
        //             that.login();
        //             var uid = wx.getStorageSync("uid");
        //             if(uid){
        //               wx.redirectTo({
        //                 url: callback_url
        //             });
        //             }
        //         } else {
        //           //用户没有授权
        //           console.log("用户没有授权");
        //         }
        //       }
        //     });
    },
    methods: {
        // bindGetUserInfo: function (e) {
        //     var that = this;
        //     uni.getUserProfile({
        //         desc: '用于完善会员资料',
        //         // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        //         success: (res) => {
        //             //存入信息
        //             uni.setStorageSync('userInfo', res.userInfo);
        //             //授权成功后,把信息插入数据库获取uid
        //             that.login(res);
        //         }
        //     });
        // },

        login: function (info) {
            var that = this;
            uni.login({
                success(res) {
                    if (res.code) {
                        app.globalData.util.request({
                            url: 'login_new',
                            data: {
                                token: that.token,
                                code: res.code,
                                info: info
                            },
                            cachetime: '0',
                            showLoading: true,
                            success: function (t) {
                                if (t.data.errno == 0) {
                                    if (!t.data.data.is_tiao) {
                                        uni.setStorageSync('uid', t.data.data.uid);
                                        var uid = uni.getStorageSync('uid');
                                        if (uid) {
                                            uni.redirectTo({
                                                url: '/wxz_wzbagent' + that.callback_url + '?token=' + that.token
                                            });
                                        }
                                    } else {
                                        /* 跳转到最新的小程序 */
                                        uni.navigateToMiniProgram({
                                            appId: t.data.data.appid,
                                            path: '/wxz_wzbagent' + that.callback_url + '?token=' + that.token,
                                            success(res) {
                                                // 打开成功
                                            }
                                        });
                                    }
                                }
                            }
                        });
                    } else {
                        console.log('登录失败！' + res.errMsg);
                    }
                }
            });
        }
    }
};
</script>
<style>
@import './login.css';
</style>
